
def print_menu():
    print("="*50)
    print("\t~学生管理系统1.0版本~")
    print("\t1:添加学生信息")
    print("\t2:删除学生信息")
    print("\t3:修改学生信息")
    print("\t4:查找学生信息")
    print("\t5:显示学生信息")
    print("\t6:退出学生信息系统")
    print("="*50)
    print("\n")

student_list = []
def add_info():
    global student_list
    # 定义一个字典
    student = {}
    name = input("请输入要添加的学生姓名：")
    phone = input("请输入要添加的学生电话：")
    wechat = input("请输入要添加的学生微信：")
    # 往字典里添加数据
    student["name"] = name
    student["phone"] = phone
    student["wechat"] = wechat
    # 把学生字典添加到列表中
    # student_list.insert(0, student)
    student_list.append(student)

    print(student_list)
    # 打印信息
    show_info()

def show_info():
    print("\n")
    print("\t\t当前学生信息")
    print("~"*70)
    print("\t姓名\t\t电话\t\t微信\t")
    for student in student_list:
        print("\t" + student["name"] + "\t\t" + student["phone"] + "\t\t" + student["wechat"] + "\t")
    print("~"*70)
    print("\n")


def del_info():
    "删除学生信息的方法"
    global student_list
    name = input("请输入要删除的学生姓名：")
    for student in student_list:
        if name == student["name"]:
            student_list.remove(student)
    # 查看学生信息
    show_info()

def mod_info():
    name = input("请输入要修改的学生姓名：")
    for student in student_list:
        if name == student["name"]:
            # 输入新的学生信息
            new_name = input("请输入新的学生姓名：")
            new_phone = input("请输入新的学生电话：")
            new_wechat = input("请输入新的学生微信：")
        student["name"] = new_name
        student["phone"] = new_phone
        student["wechat"] = new_wechat
        # 查看学生信息
        show_info()

def find_info():
    name = input("请输入要修改的学生姓名：")
    flag = False
    for student in student_list:
        if name == student["name"]:
            print("已经查询到%s" % name)
            flag = True
        
    if flag == False:
        print("没有查询到%s" % name)
         
def save_data():
    global student_list
    # 添加、删除、修改时保存信息
    # 在程序运行时读取信息
    f = open("stu.data", "w")
    f.write(str(student_list))
    f.close


def read_data():
    global student_list
    f = open("stu.data", "a+")
    f.seek(0)
    content = f.read()
    print(content)
    if len(content) > 0:
        student_list = eval(content)
    f.close()



def main():
    print_menu()
    read_data()
    print("\n")
    while True:
        print("\t\t操作指令")
        print("~"*50)
        infos =  "1:增加|2:删除|3:修改|4:查找|5:显示|6:退出系统"
        print(infos)
        print("~"*50)
        num = input("请按照上面的提示输入相应指令：")
        # print(num)
        # 判断用户输入的内容:1、2、3、4、5、6
        if num == "1":
            print("添加")
            add_info()
            save_data()
        elif num == "2":
            print("删除")
            del_info()
            save_data()
        elif num == "3":
            print("修改")
            mod_info()
            save_data()
        elif num == "4":
            print("查询")
            find_info()
        elif num == "5":
            print("显示")
            show_info()
        elif num == "6":
            print("退出")
            break

# 调用main函数
main()